package org.meichao.hello.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.meichao.config.GlobalException;
import org.meichao.config.annotation.Sign;
import org.meichao.hello.service.HelloService;
import org.meichao.thread.MultiTaskPool;
import org.meichao.utils.CommonUtils;
import org.meichao.utils.RespInfoUtils;
import org.meichao.utils.vo.RespInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/hello")
@RefreshScope
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private HelloService helloService;

    @Sign
    @RequestMapping("/hello")
    public RespInfo hello(HttpServletRequest request) throws GlobalException, ExecutionException, InterruptedException {

        Future<List<JSONObject>> listFuture = MultiTaskPool.getInstance().executeTaskReturn(
                new Callable<List<JSONObject>>() {
                    @Override
                    public List<JSONObject> call() throws Exception {
                        return helloService.getUser();
                    }
        });

        String userKey = CommonUtils.getCurrentUserKey(request);
        if (StringUtils.isNotEmpty(userKey)){
            logger.info("token:{}", userKey);
        }
        return RespInfoUtils.create().respSuccess(listFuture.get()).toRespInfo();
    }

    public static void main(String[] args) {
        Map<String, String> map1 = new HashMap<>();
        map1.put("2020-11-30", "2020-11-30");
        map1.put("2020-11-25", "2020-11-25");
        Map<String, String> map2 = new HashMap<>();
        map2.put("2020-11-12", "2020-11-12");
        map2.put("2020-11-25", "2020-11-2522222222");
        Map<String, String> map3 = new HashMap<>();
        map3.put("2020-11-06", "2020-11-06");
        map3.put("2020-11-25", "2020-11-25333333");

        //加入到List里
        List<Map> mapList = new ArrayList<>();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        System.out.println(mapList);

        //合并map并按key倒叙
        Map<Object, List> map = new TreeMap(Comparator.reverseOrder());
        for(Map m: mapList){
            Iterator it = m.keySet().iterator();
            while(it.hasNext()){
                Object key = it.next();
                if(map.containsKey(key)){
                    //key已存在,把value加入list中
                    map.get(key).add(m.get(key));
                }else{
                    //key不存在,把value加入新建list中
                    List valueList = new ArrayList();
                    valueList.add(m.get(key));
                    map.put(key, valueList);
                }
            }
        }
        System.out.println(map);
    }


}
